#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define N 100007
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        ll n, m;
        cin >> n >> m;
        if (n == 0 || m == 0)
        {
            cout << 0 << endl;
            continue;
        }
        if (n == 1 && m == 1)
        {
            cout << 1 << endl;
            continue;
        }
        if (n == 1 || m == 1)
        {
            cout << 2 << endl;
            continue;
        }
        cout << 4 << endl;
    }
    return 0;
}